<?php
/**
 * Created by JetBrains PhpStorm.
 * User: xjw129xjt
 * Date: 13-8-19
 * Time: 下午8:57
 * To change this template use File | Settings | File Templates.
 */

class FriendModel extends Model
{
//获取分组内成员数量
    function getcount($db, $condition)
    {
        $result = D($db)->where($condition)->count();
        if (!$result) {
            return 0;
        }
        return $result;
    }

    //通过分组ID获取分组名字
    function  getGroupNamebyGroupid($groupid)
    {
        if ($groupid == -1) {
            return '全部好友';
        }
        if ($groupid == 0) {
            return '未分组';
        }
        $condition['id'] = $groupid;
        $group = D('friend_group')->where($condition)->select();
        return $group['0']['groupname'];
    }

    function  getCityNamebyid($id)
    {

        $condition['area_id'] = $id;
        $area = D('area')->where($condition)->select();
        return $area['0']['title'];
    }

    function  getSchoolNamebySchoolid($schoolid)
    {

        $condition['school_id'] = $schoolid;
        $school = D('school')->where($condition)->select();
        return $school['0']['name'];
    }



    //通过分组ID，用户ID获取用户分组的好友
    function getFriendListbyGroupid($groupid, $uid)
    {
        if ($groupid == -1) {
            $condition1['uid'] = $uid;
            $user = D('friend')->where($condition1)->order('fname asc')->select();
        }
        if ($groupid == 0) {
            $condition_wei['uid'] = $uid;
            $condition_wei['groupid'] = 0;
            $user = D('friend')->where($condition_wei)->order('fname asc')->select();
        } elseif ($groupid > 0) {
            $condition['uid'] = $uid;
            $condition['groupid'] = $groupid;
            $user = D('friend')->where($condition)->order('fname asc')->select();
        }
        foreach ($user as $key => $v) {
            $user_info[$key] = D('User')->getUserInfo($v['fid']);
            $condition2['uid'] = $v['fid'];
            $user_info[$key]['friend_info'] = D('friend_info')->where($condition2)->select();
            $user_info[$key]['groupname'] = $this->getGroupNamebyGroupid($v['groupid']);
        }
        return $user_info;
    }


    //加好友
    function addfriend($uid, $fid)
    {
        $accept = D('friend');
        $data['uid'] = $uid;
        $data['fid'] = $fid;
        $data['groupid'] = 0;
        /*      $user = D('User')->getUserInfo($fid);*/
        $map['uid'] = $fid;
        $user = D('friend_info')->where($map)->select();
        $data['fname'] = $user['0']['name'];
        $accept->data($data)->add();

    }

    //查看数据库是否已有记录
    function checkdb($db, $condition)
    {
        $result = D($db)->where($condition)->select;
        return $result;
    }

    function getAreaName($id)
    {
        $condition['area_id'] = $id;
        $province = D('area')->where($condition)->select();
        return $province['0']['title'];

    }

    function getxingzuoid($name)
    {
        $condition['xname'] = $name;
        $xid = D('friend_xingzuo')->where($condition)->select();
        return $xid['0']['xid'];

    }


    function getschoollist($id)
    {
        $condition['uid'] = $id;
        $t = D('friend_school')->where($condition)->select();
        foreach ($t as $k => $v) {
            $result[$k] = $v['school_name'];
        }
        return $result;
    }

    function getcitylist($id)
    {
        $condition['uid'] = $id;
        $t = D('friend_info')->where($condition)->select();

        $result['0'] = $t['0']['now_city'];
        $result['1'] = $t['0']['home_city'];

        return $result;
    }

    function checkisfriend($fids, $id)
    {
        foreach ($fids as $k => $v) {
            if ($id == $v['fid']) {
                $res = false;
                break;
            } else {
                $res = true;
            }

        }
        return $res;
    }

    function getconnection($uid, $fid)
    {
        $condition['uid'] = $fid;
        $condition1['uid'] = $uid;
        $a = D('friend')->where($condition)->select();
        $b = D('friend')->where($condition1)->select();
        foreach ($b as $k => $v) {
            $ids[$k] = $v['fid'];
        }
        foreach ($a as $k => $v) {
            $x = $this->checkisfriend($ids, $v['fid']);
            if ($x == true && $v['fid'] != $uid) {
                $result[$k] = $v;
            }
        }

        return $result;
    }

    function getSameFriend($uid, $id)
    {
        $condition['uid'] = $uid;
        $a = D('friend')->where($condition)->select();
        foreach ($a as $k => $v) {
            $x[$k] = $v['fid'];
        }
        $condition1['uid'] = $id;
        $b = D('friend')->where($condition1)->select();
        foreach ($b as $k => $v) {
            $y[$k] = $v['fid'];
        }
        $z = array_merge($x, $y);
        $h = array_count_values($z);
        $g = array();
        foreach ($h as $k => $v) {
            if ($v >= 2 && $k != $uid) {
                array_push($g, $k);
            }
        }
        foreach ($g as $k => $v) {
            $result[$k] = D('User')->getUserInfo($v);
            $school = D('friend_school')->where('uid=' . $v)->select();
            $result[$k]['school'] = $school;
            $info = D('friend_info')->where('uid = ' . $v)->select();
            $result[$k]['name'] = $info['0']['name'];
        }
        return $result;
    }


    function get_one_connection($uid)
    {

        $condition['uid'] = $uid;
        $friend = D('friend')->where($condition)->select();
        foreach ($friend as $k => $v) {
            $map['uid'] = $v['fid'];
            $a = D('friend_info')->where($map)->select();
            $user[$k] = $a['0'];
            $temp = D('User')->getUserInfo($v['fid']);
            $user[$k]['head'] = $temp['avatar_middle'];
            $user[$k]['now_cityname'] = $this->getCityNamebyid($a['0']['now_province']) . $this->getCityNamebyid($a['0']['now_city']);
            $user[$k]['home_cityname'] = $this->getCityNamebyid($a['0']['home_province']) . $this->getCityNamebyid($a['0']['home_city']);
            $user[$k]['school'] = D('friend_school')->where($map)->select();

            $user[$k]['connection'] = $this->getconnection($uid, $v['fid']);
            $user[$k]['connection']['count'] = count($user[$k]['connection']);
        }
        return $user;
    }

    function get_two_connection($uid)
    {

        $condition['uid'] = $uid;
        $friend = D('friend')->where($condition)->select();
        $f = array();
        foreach ($friend as $k => $v) {
            $ids[$k] = $v['fid'];
            $map['uid'] = $v['fid'];
            $t[$k] = D('friend')->where($map)->select();
            foreach ($t[$k] as $key => $val) {
                $t1[$k][$key] = $val['fid'];
            }
        }
        foreach ($t1 as $k => $v) {
            $f = array_merge($f, $t1[$k]);
            $g = array_unique($f);
        }

        foreach ($g as $k => $v) {
            $x = $this->checkisfriend($ids, $v);
            if ($x == true && $v != $uid) {
                $result[$k] = $v;
            }
        }
        foreach ($result as $k => $v) {
            $map1['uid'] = $v;
            $a = D('friend_info')->where($map1)->select();
            $user[$k] = $a['0'];
            $temp = D('User')->getUserInfo($v);
            $user[$k]['head'] = $temp['avatar_middle'];
            $user[$k]['now_cityname'] = $this->getCityNamebyid($a['0']['now_province']) . $this->getCityNamebyid($a['0']['now_city']);
            $user[$k]['home_cityname'] = $this->getCityNamebyid($a['0']['home_province']) . $this->getCityNamebyid($a['0']['home_city']);
            $user[$k]['school'] = D('friend_school')->where($map1)->select();
            $user[$k]['same'] = $this->getSameFriend($uid, $v);
            $user[$k]['same_count'] = count($user[$k]['same']);
        }

        return $user;
    }

}